let i = 0;

setInterval(() => {
  if (i >= 3) {
    self.close();   //与主线程没有通讯自动删除
  } else {
    i++;
    self.postMessage("sub");
  }
}, 1000);

self.addEventListener('message', (e) => {
  if (e.data == "next") {
    requestAnimationFrame(() => {
      self.postMessage("run");
    });
  }
  else if (e.data == "main") {
    i = 0;
  }
}, true);